package org.dregs.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class TimeUtil {

    public static final long ONE_DAY_TIME = 24 * 60 * 60 * 1000l;

    public static final long ONE_MINUTE_TIME = 60 * 1000l;

    public static final long ONE_HOUR_TIME = 60 * 60 * 1000l;

    public static final long ONE_HOUR_HALF_TIME = 30 * 60 * 1000l;

    public static final String DATE_FORMAT_NUM_yMd = "yyyyMMdd";

    public static final String DATE_FORMAT_STR_yMd = "yyyy:MM:dd";

    public static final String TIME_FORMAT_NUM_yMdhmsS = "yyyyMMddHHmmssSSS";

    public static final String TIME_FORMAT_STR_yMdhmsS = "yyyy-MM-dd HH:mm:ss.SSS";

    public static final String TIME_FORMAT_STR_yMdHHmm = "yyyyMMddHHmm";
    public static final String TIME_FORMAT_STR_yMdHH = "yyyyMMddHH";


    public static String getString(long time, String format){
        return new SimpleDateFormat(format).format(time);
    }
    public static String getString(Date date, String format){
        return new SimpleDateFormat(format).format(date);
    }
    public static Date stringToDate(String string, String format) throws ParseException {
        return new SimpleDateFormat(format).parse(string);
    }

    public static Date zeroDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }
    public static long zeroTime(long time){
        return time / (1000*3600*24) * (1000*3600*24) - TimeZone.getDefault().getRawOffset();
    }
    public static Date toDayZeroDate(){
        return zeroDate(new Date());
    }
    public static long toDayZeroTime(){
        return zeroTime(System.currentTimeMillis());
    }
    public static Date nextDayZeroDate(){
        return zeroDate(new Date(System.currentTimeMillis() + (1*ONE_DAY_TIME)));
    }
    public static long nextDayZeroTime(){
        return zeroTime(System.currentTimeMillis() + (1*ONE_DAY_TIME));
    }
    public static Date preDayZeroDate(){
        return zeroDate(new Date(System.currentTimeMillis() - (1*ONE_DAY_TIME)));
    }
    public static long preDayZeroTime(){
        return zeroTime(System.currentTimeMillis() - (1*ONE_DAY_TIME));
    }
    public static Date assignDate(int dayNum){
        return zeroDate(new Date(System.currentTimeMillis() + (dayNum*ONE_DAY_TIME)));
    }
    public static long assignTime(int dayNum){
        return zeroTime(System.currentTimeMillis() + (dayNum*ONE_DAY_TIME));
    }

    public static String nowTime(long time){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
    }
    public static String nowTime(){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis());
    }
    public static long getMinuteLongValue(int minuteNum){
        return minuteNum * ONE_MINUTE_TIME;
    }
    public static long getHourLongValue(double minuteNum){
        return ((int)(minuteNum*2)) * ONE_HOUR_HALF_TIME;
    }
    public static long getHourLongValue(int minuteNum){
        return minuteNum * ONE_HOUR_TIME;
    }

}
